package jp.eisbahn.eclipse.plugins.lingrclipse.view;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class EnterRoomDialog extends Dialog {

	private Text roomNameText;
	private Label errorLabel;
	private Text nickNameText;
	
	private String roomName;
	private String nickName;

	protected EnterRoomDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite)super.createDialogArea(parent);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);
		//
		Label roomNameLabel = new Label(composite, SWT.NONE);
		roomNameLabel.setText("Room name:");
		//
		roomNameText = new Text(composite, SWT.SINGLE | SWT.BORDER);
		GridData layoutData = new GridData();
		layoutData.grabExcessHorizontalSpace = true;
		layoutData.horizontalAlignment = GridData.FILL;
		roomNameText.setLayoutData(layoutData);
		roomNameText.setText("");
		//
		Label nickNameLabel = new Label(composite, SWT.NONE);
		nickNameLabel.setText("Nickname:");
		//
		nickNameText = new Text(composite, SWT.SINGLE | SWT.BORDER);
		layoutData = new GridData();
		layoutData.grabExcessHorizontalSpace = true;
		layoutData.horizontalAlignment = GridData.FILL;
		nickNameText.setLayoutData(layoutData);
		String systemUserName = System.getProperty("user.name");
		nickNameText.setText(systemUserName);
		//
		errorLabel = new Label(composite, SWT.NONE);
		errorLabel.setForeground(Activator.getDefault().getColorProvider().getColor(ColorProvider.RED));
		layoutData = new GridData();
		layoutData.grabExcessHorizontalSpace = true;
		layoutData.horizontalAlignment = GridData.FILL;
		layoutData.horizontalSpan = 2;
		errorLabel.setLayoutData(layoutData);
		//
		return composite;
	}

	@Override
	protected void okPressed() {
		String roomName = roomNameText.getText().trim();
		String nickName = nickNameText.getText().trim();
		if (StringUtils.isEmpty(roomName)) {
			errorLabel.setText("Room name is empty.");
			roomNameText.setText(roomName);
		} else if (StringUtils.isEmpty(nickName)) {
			errorLabel.setText("Nickname is empty.");
			nickNameText.setText(nickName);
		} else {
			this.roomName = roomName;
			this.nickName = nickName;
			setReturnCode(OK);
			close();
		}
	}
	
	public String getRoomName() {
		return roomName;
	}
	
	public String getNickName() {
		return nickName;
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Enter room");
	}

}
